Conditions | 6 |
Total Lines | 159 |
Code Lines | 107 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | import { useState, useEffect } from 'react'; |
||
14 | |||
15 | export default function Map({navigation, API_KEY, position, setPosition, token}): any { |
||
16 | const [locationMarker, setLocationMarker] = useState(null); |
||
17 | const [highlight, setHighlight] = useState(null); |
||
18 | const [currentCity, setCurrentCity] = useState(null); |
||
19 | const [zones, setZones] = useState([]); |
||
20 | const [scooters, setScooters] = useState([]); |
||
21 | const [currentScooter, setCurrentScooter] = useState(null); |
||
22 | const [modalVisible, setModalVisible] = useState(false); |
||
23 | const [zoneModalVisible, setZoneModalVisible] = useState(false); |
||
24 | const [currentZone, setCurrentZone] = useState(null); |
||
25 | |||
26 | |||
27 | /** |
||
28 | * Set user position |
||
29 | */ |
||
30 | useEffect(() => { |
||
31 | async function fetchPosition(): Promise<void> { |
||
32 | const { status } = await Location.requestForegroundPermissionsAsync(); |
||
33 | |||
34 | // if (status !== 'granted') { |
||
35 | // setErrorMessage('Permission to access location was denied'); |
||
36 | // return; |
||
37 | // } |
||
38 | |||
39 | const currentLocation = await Location.getCurrentPositionAsync({}); |
||
40 | |||
41 | const userCoordinates = { |
||
42 | //latlang hardcoded for testing |
||
43 | // latitude: currentLocation.coords.latitude, |
||
44 | // longitude: currentLocation.coords.longitude |
||
45 | latitude: 56.161013580817986, |
||
46 | longitude: 15.587742977884904 |
||
47 | }; |
||
48 | |||
49 | |||
50 | setPosition(userCoordinates); |
||
51 | |||
52 | mapModel.getClosestCity(position); |
||
53 | |||
54 | setLocationMarker(<Marker |
||
55 | coordinate={{ |
||
56 | //latlang hardcoded for testing |
||
57 | // latitude: currentLocation.coords.latitude, |
||
58 | // longitude: currentLocation.coords.longitude |
||
59 | latitude: 56.161013580817986, |
||
60 | longitude: 15.587742977884904 |
||
61 | }} |
||
62 | title="My location" |
||
63 | pinColor="blue" |
||
64 | flat={false} |
||
65 | />); |
||
66 | }; |
||
67 | |||
68 | |||
69 | fetchPosition(); |
||
70 | |||
71 | }, []); |
||
72 | |||
73 | /** |
||
74 | * Set city to city that is closest to user and zones for that city |
||
75 | */ |
||
76 | useEffect(() => { |
||
77 | async function setUpMap(): Promise<void> { |
||
78 | const city = await mapModel.getClosestCity(position); |
||
79 | |||
80 | |||
81 | // Set city that is closest to user |
||
82 | setCurrentCity(city); |
||
83 | |||
84 | /** |
||
85 | * Set zones on map |
||
86 | */ |
||
87 | const zones = mapModel.getZones(city); |
||
88 | setZones(zones); |
||
89 | |||
90 | |||
91 | /** |
||
92 | * Get all scooters and create markers for them on the map |
||
93 | */ |
||
94 | const result = await scooterModel.getScooters(API_KEY, city); |
||
95 | |||
96 | const scooters = result['cityScooters']; |
||
97 | const sortedScooters = scooterModel.sortAvailableScooters(scooters); |
||
98 | // console.log(scooters[0]); |
||
99 | |||
100 | // console.log(sortedScooters[0]); |
||
101 | |||
102 | setScooters(sortedScooters); |
||
103 | |||
104 | }; |
||
105 | setUpMap(); |
||
106 | }, []); |
||
107 | |||
108 | |||
109 | function DrawerButton({navigation}) { |
||
110 | return ( |
||
111 | <Pressable style={[styles.drawer, styles.shadowProp]} onPress={() => navigation.openDrawer()}> |
||
112 | <Icon |
||
113 | name='three-bars' |
||
114 | size={30} |
||
115 | color='black' |
||
116 | /> |
||
117 | </Pressable> |
||
118 | ); |
||
119 | }; |
||
120 | |||
121 | return ( |
||
122 | <View style={styles.container}> |
||
123 | <MapView |
||
124 | style={styles.map} |
||
125 | region={{ |
||
126 | latitude: position.latitude? position.latitude : 0, |
||
127 | longitude: position.longitude? position.longitude : 0, |
||
128 | latitudeDelta: 0.03, |
||
129 | longitudeDelta: 0.03, |
||
130 | }} |
||
131 | userInterfaceStyle={'dark'} |
||
132 | > |
||
133 | {locationMarker} |
||
134 | |||
135 | {scooters.map((s, index) => |
||
136 | <Marker |
||
137 | // title={s['name']} |
||
138 | // description={`Charge ${s['battery']}% ${s['status']}`} |
||
139 | coordinate={s['coordinates']} |
||
140 | icon={require('../assets/Scooter1.png')} |
||
141 | tappable={true} |
||
142 | key={index} |
||
143 | onPress={() => { |
||
144 | setCurrentScooter(s) |
||
145 | setModalVisible(true) |
||
146 | }} |
||
147 | > |
||
148 | </Marker> |
||
149 | )} |
||
150 | {zones.map((z, index) => ( |
||
151 | <Polygon |
||
152 | coordinates={z['coordinates']} |
||
153 | strokeColor={z['zoneColor']} |
||
154 | strokeWidth={3} |
||
155 | fillColor={z['zoneColor']} |
||
156 | key={index} |
||
157 | tappable={true} |
||
158 | onPress={() => { |
||
159 | setCurrentZone(z) |
||
160 | setZoneModalVisible(true) |
||
161 | }} |
||
162 | /> |
||
163 | ))} |
||
164 | </MapView> |
||
165 | |||
166 | |||
167 | <ScooterModal navigation={navigation} scooter={currentScooter} modalVisible={modalVisible} currentCity={currentCity} setModalVisible={setModalVisible} /> |
||
168 | |||
169 | <ZoneModal navigation={navigation} zone={currentZone} zoneModalVisible={zoneModalVisible} setZoneModalVisible={setZoneModalVisible} /> |
||
170 | |||
171 | <NavBar navigation={navigation} /> |
||
172 | </View> |
||
173 | ) |
||
210 |